package com.hjw.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.hjw.entity.Order;
import com.hjw.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 * @创建时间：2025-04-21
 * @文件名称：satoken_sobuk - com.hjw.controller
 */
@Slf4j
@SaCheckRole("usr")
@RestController
@RequestMapping("/api/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @PostMapping("/findMyOrder") // /api/order/findMyOrder
    public SaResult findMyOrder(@RequestBody Map<String, Object> map) {
        int uid = StpUtil.getLoginIdAsInt();

        Map<String, Object> m = new HashMap<>(1);
        m.put("uid", uid);

        if (map.get("tpay") != null){
            int tpay = Integer.parseInt(map.get("tpay").toString());
            m.put("tpay", tpay);
        }
        //获取过去x天的日期
        if (map.get("pay") != null && !map.get("pay").toString().isEmpty()){
            int payStatus = Integer.parseInt(map.get("pay").toString());
            m.put("payStatus", payStatus);
        }
        if (map.get("ship") != null && !map.get("ship").toString().isEmpty()){
            int shipStatus = Integer.parseInt(map.get("ship").toString());
            m.put("shipStatus", shipStatus);
        }
        log.info("map=" + m);

        List<Order> orders = orderService.findMyOrder(m);

        int lines = Integer.parseInt(map.get("lines").toString());

        int total = orders.size();
        //过滤查询提取
        if( lines > orders.size()){
            lines = orders.size();
        }

        Map<String, Object> returnMap = new HashMap<>(2);
        returnMap.put("total", total);
        returnMap.put("orders", orders.subList(0, lines));
        return SaResult.get(200, "获取用户订单成功", returnMap);
    }

    @GetMapping("/findByOrdId/{id}") // /api/order/findByOrdId/{id}
    public SaResult findByOrdid(@PathVariable("id") Integer id) {
        if (id == null){
            return SaResult.get(-200, "订单id为空", null);
        }
        log.info("查询订单id={}", id);
        int uid = StpUtil.getLoginIdAsInt();
        Map<String, Object> params = new HashMap<>(2);
        params.put("uid",uid);
        params.put("ordid", id);
        log.info("查询条件：{}", params);
        Order order = orderService.findByOrdId(params);
        return SaResult.get(200, "获取订单成功", order);
    }

    @GetMapping("/addPay/{id}")
    public SaResult addPay(@PathVariable("id") Integer ordid) {
        int uid = StpUtil.getLoginIdAsInt();
        Map<String, Object> map = new HashMap<>(2);
        map.put("payStatus", 1);
        map.put("ordid", ordid);
        map.put("uid", uid);

        int i = orderService.update(map);
        //修改完成后，进行查询返回的更新结果
        if (i == 0){
            return SaResult.get(-200, "订单不存在", null);
        }
        Order order = orderService.findByOrdId(map);
        return SaResult.get(200, "支付成功", order);
    }


}
